<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 资金账户模型
 * @author www.dingwei.cn
 */

class MerchantBalanceModel extends MY_Model{
    public $tbUser = 'merchant';
    public $tbShop = 'merchant_shop';
    public $tbBalance = 'merchant_balance_log';
    public $config = array(
        'type'=>array('1' => '充值','2' => '支付','3' => '退款','4' => '提现','5' => '收入','6' => '发红包','7' => '平台调整'),
        'pay_type'=>array('1' => '余额','2' => '微信','3' => '支付宝','4' => '银行卡','5'=>'平台调整','6' => '线下打款'),
        'status'=>array('1' => '失败','2' => '成功','3' => '申请中'),
    );

    /**
     * 保存
     *
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     * @author khj
     */
    public function save($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('id' => $id));
            if($this->update($this->tbBalance, $data)){
                return true;
            }
        }else{
            if($id = $this->insert($this->tbBalance, $data)){
                return $id;
            }
        }
    }

    /**
     *  获取资金明细
     * @param $merchantId  商户id
     * @param $page        页码
     * @param $year        年份
     * @param $month       月份
     * @return array
     */
    public function appGetlist($merchantId, $page , $year, $month){
        if(!$merchantId)return array();

        if($year && $month){//筛选
            $start_date=date("Y-m-d H:i:s", mktime(0, 0 , 0,$month,1,$year));
            $end=strtotime("$start_date +1 month");
            $start=strtotime($start_date);
           if(is_int($end) && is_int($start)){
               $this->db->where(array('addtime >=' =>$start, 'addtime <' => $end));
           }
        }

        $this->db->select('type,amount,addtime,status')
                 ->where('merchant_id', $merchantId)
                 ->order_by('addtime desc');

        $tA = $this->appPage($this->tbBalance, $page);
        $status = $this->config;

        foreach ($tA['list'] as $k=>&$v){
            $v['addtime'] = dateFormat($v['addtime']);
            $v['status'] = $status['status'][$v['status']];
            $v['type'] = $status['type'][$v['type']];
        }

        return $tA;
    }

    /**
     * 后台获取列表
     *
     * @param  array    $ci
     * @param  array    $export
     * @return array
     * @author khj
     */
	public function adminGetList($ci = array(), $export = array()){
        $merchantId = intval(get('merchant_id'));
        $type = intval(get('found_type'));
        $pay_type = intval(get('pay_type'));
        $status = intval(get('found_status'));
        $keyword = trim(get('keyword'));
        $amount = trim(get('amount'));
        $confirm_time_from = strtotime(get('confirm_time_from'));
        $confirm_time_to = strtotime(get('confirm_time_to'));
        $addtime_from = strtotime(get('addtime_from'));
        $addtime_to = strtotime(get('addtime_to'));

        if (!empty($ci)){
            $this->db->where_in('s.county', $ci);
        }
        $iSortCol_0 = get('iSortCol_0');
        $sSortDir_0 = get('sSortDir_0');

        if ($iSortCol_0 == 1){
            $this->db->order_by('b.amount '.$sSortDir_0);
        }

        if ($iSortCol_0 == 3){
            $this->db->order_by('b.addtime '.$sSortDir_0);
        }

        if($type){
            $this->db->where('b.type', $type);
        }
        if($pay_type){
            $this->db->where('b.pay_type', $pay_type);
        }

        if($status){
            $this->db->where('b.status', $status);
        }
        if ($merchantId){
            $this->db->where(array('b.merchant_id' => $merchantId));
        }
        if ($keyword){
            $this->db->where('u.nickname like "%'.$keyword.'%" or u.mobile like "%'.$keyword.'%"');
        }
        if ($amount){
            $this->db->like(array('b.amount' => $amount));
        }
        if($confirm_time_from){
            $this->db->where('b.confirm_time >=', $confirm_time_from);
        }
        if($confirm_time_to){
            $this->db->where('b.confirm_time <', strtotime('+1day', $confirm_time_to));
        }
        if($addtime_from){
            $this->db->where('b.addtime >=', $addtime_from);
        }
        if($addtime_to){
            $this->db->where('b.addtime <', strtotime('+1day', $addtime_to));
        }

	    $this->db->select('b.id,b.param,b.merchant_id,b.type,b.pay_type,b.amount,b.status,b.reason,b.addtime,b.confirm_time,s.shop_name')
                 ->join($this->tbUser.' u','u.id = b.merchant_id')
                 ->join($this->tbShop.' s','u.id = s.merchant_id', 'left')
                 ->order_by('addtime desc');

        if (empty($export)){
            return $this->pagination($this->tbBalance.' b');
        }else{
            $tA = $this->appPage($this->tbBalance.' b', $export['page'], false, $export['pageNumber']);
            return $tA['list'];
        }
    }

    /**
     * 获取数据详情
     * @param $id           int
     * @param $orderId      int     订单id
     * @param $type         int     消费类型
     * @return array
     * @author heyu
     */
    public function getInfo($id = 0, $orderId = 0, $type = 0){
        if (!$id && (!$orderId || !$type)){
            return array();
        }
        if($id){
            $this->db->where(array('id' => $id));
        }
        if ($orderId){
            $this->db->where(array('param' => $orderId))
                ->where(array('type' => $type));
        }
        $tA = $this->getOne($this->tbBalance);
        return $tA;
    }

    /**
     * 获取提现总金额 总次数
     * @param int $startTime
     * @param int $endTime
     * @param bool $isOne
     * @param int $page
     * @param int $pageNumber
     * @return array
     * @author heyu
     */
    public function getTotalBalanceLog($startTime = 0, $endTime = 0, $isOne = true, $page = 0, $pageNumber = 0){
        if ($startTime){
            $this->db->where('addtime >= '.$startTime);
        }
        if ($endTime){
            $this->db->where('addtime < '.$endTime);
        }
        $this->db->select('sum(if(type=4, amount, null)) as withdrawals,FROM_UNIXTIME(addtime,"%Y-%m-%d") as time')
            ->where(array('status' => 2, 'type' => 4));
        if ($isOne){
            return $this->getOne($this->tbBalance);
        }else{
            $this->db->group_by('time')
                ->order_by('time desc');
            $data = array();
            if ($page && $pageNumber) {
                $tA = $this->appPage($this->tbBalance, $page, false, $pageNumber);
                if (!empty($tA['list'])){
                    foreach ($tA['list'] as $v){
                        $data[$v['time']] = $v;
                    }
                }
            }else {
                $tA = $this->pagination($this->tbBalance);
                if (!empty($tA['data'])){
                    foreach ($tA['data'] as $v){
                        $data[$v['time']] = $v;
                    }
                }
            }

            unset($tA);
            return $data;
        }
    }
}